package com.milk.servlet;

import com.milk.Dao.StockDao;
import com.milk.entity.Stock;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class StockManagementServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String action = request.getPathInfo().substring(1);
        switch (action) {
            case "viewAll":
                listAllStock(request, response);
                break;
            case "viewById":
                viewStockByDrinkId(request, response);
                break;
            case "update":
                showUpdateStockForm(request, response);
                break;
            case "lowAlert":
                showLowStockAlert(request, response);
                break;
            default:
                response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String action = request.getPathInfo().substring(1);
        switch (action) {
            case "update":
                updateStockQuantity(request, response);
                break;
            default:
                response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        }
    }

    private void listAllStock(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Stock> stocks = StockDao.findAll();
        request.setAttribute("stocks", stocks);
        request.getRequestDispatcher("/stock_management.jsp").forward(request, response);
    }

    private void viewStockByDrinkId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String drinkIdParam = request.getParameter("drinkId");
        if (drinkIdParam == null || drinkIdParam.isEmpty()) {
            response.sendRedirect(request.getContextPath() + "/stock/viewAll?error=missing_id");
            return;
        }

        try {
            int drinkId = Integer.parseInt(drinkIdParam);
            Stock stock = StockDao.findByDrinkId(drinkId);
            request.setAttribute("stock", stock);
            request.getRequestDispatcher("/view_stock_by_id.jsp").forward(request, response);
        } catch (NumberFormatException e) {
            response.sendRedirect(request.getContextPath() + "/stock/viewAll?error=invalid_id");
        }

    }

    private void showUpdateStockForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/update_stock_form.jsp").forward(request, response);
    }

    private void updateStockQuantity(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String drinkIdParam = request.getParameter("drinkId");
        String newQuantityParam = request.getParameter("newQuantity");

        // Check if parameters are empty
        if (drinkIdParam == null || drinkIdParam.isEmpty() || newQuantityParam == null || newQuantityParam.isEmpty()) {
            response.sendRedirect(request.getContextPath() + "/stock/update?error=missing_parameters");
            return;
        }

        try {
            int drinkId = Integer.parseInt(drinkIdParam);
            int newQuantity = Integer.parseInt(newQuantityParam);

            int result = StockDao.updateQuantity(drinkId, newQuantity);
            if (result > 0) {
                response.sendRedirect(request.getContextPath() + "/stock/viewAll");
            } else {
                response.sendRedirect(request.getContextPath() + "/stock/update?error=failed");
            }
        } catch (NumberFormatException e) {
            // Handle the case where the parameters are not valid integers
            response.sendRedirect(request.getContextPath() + "/stock/update?error=invalid_parameters");
        }
    }

    private void showLowStockAlert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Stock> lowStockList = StockDao.getLowStockAlert();
        request.setAttribute("lowStockList", lowStockList);
        request.getRequestDispatcher("/low_stock_alert.jsp").forward(request, response);
    }
}